import Vue from 'vue'
import App from './App.vue'
import "babel-polyfill";
import axios from 'axios'
import VueAxios from 'vue-axios'
import VueCookie from 'vue-cookie'


axios.defaults.baseURL = '/api';
axios.defaults.timeout = 8000;

axios.interceptors.response.use(function(response){
    let res = response.data;
    if(res.errcode == 0){
        return res.data;
    }else if(res.errcode == 401){
        Message.warning(res.errmsg);
        window.location.href = '/#/login';
        return Promise.reject(res);
    }else{
        Message.warning(res.errmsg);
        return Promise.reject(res);
    }
},(error)=>{
    let res = error.response;
    Message.error(res.data.message);
    return Promise.reject(error);
});
//全局注入axios对象，使用时直接this.axios()
Vue.use(VueAxios,axios);
Vue.use(VueCookie);

//elementui使用
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';    // 默认主题
Vue.use(ElementUI, { size: 'small' });

//vue router使用
import routes from './router/routes'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
    routes
})
Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
    next()
    // if (to.path == '/login') {
    //     sessionStorage.removeItem('mockUser');
    //     next();
    // }
    /**
     * sessionStorage 用于临时保存同一窗口(或标签页)的数据，在关闭窗口或标签页之后将会删除这些数据
     * localStorage的生命周期是永久的，意思就是如果不主动清除，存储的数据将一直被保存
     */
        //let user = JSON.parse(localStorage.getItem('mockUser'));
    // let user = JSON.parse(sessionStorage.getItem('mockUser'));
    // if (user!=null&&to.path!='/login') {
    //     next();
    // }else{
    //     next({path: '/login'});
    // }
})



new Vue({
    router,
    render: h => h(App),
}).$mount('#app')
